package com.ch999.x5demo;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.artifex.mupdfdemo.MuPDFActivity;
import com.scorpio.mylib.App;
import com.scorpio.mylib.Tools.Logs;
import com.scorpio.mylib.http.MyHttp;
import com.scorpio.mylib.http.iface.DownLoadHandler;

import java.util.Date;



public class MainActivity extends AppCompatActivity {

//    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        webView = (WebView) findViewById(R.id.webview);
//
//        webView.loadUrl("http://mobilehtml5.org/ts/?id=23");


        MyHttp.inisWithCookie(getApplicationContext(), "https://m.9ji.com/app/2_0/ProductSearch.aspx", ".9ji.com", false);
//
//
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                downloadAndViewPdf("http://78rc7f.com1.z0.glb.clouddn.com/kotlin-for-android-developers-zh.pdf", getExternalCacheDir().getPath() + (new Date()).getTime() + ".pdf");

            }
        });
    }


    public void downloadAndViewPdf(String url, String path) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
        dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
        dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
        dialog.setTitle("加载中");
        dialog.setMax(100);
        dialog.show();
        //下载
        MyHttp.download(url, path, new DownLoadHandler() {
            @Override
            public void onSuccess(String s) {
                Logs.Debug("pdf下载成功：" + s);
                dialog.dismiss();
                Uri uri = Uri.parse(s);
                Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(uri);
                startActivity(intent);
            }

            @Override
            public void onFailure(Throwable throwable) {
                Logs.Debug("pdf下载失败：" + throwable.toString());
                App.toast("pdf下载失败");
                dialog.dismiss();
            }

            @Override
            public void onProgress(int i) {
                Logs.Debug("pdf下载进度：" + i);
                dialog.incrementProgressBy(i);
            }
        });

    }
}
